/**
 * @file adc_config.c
 * @author your name (you@domain.com)
 * @brief 采用stm32MXcube生成的adc驱动，使用了ADC1，4个通道，DMA传输
 * @version 0.1
 * @date 2022-03-15
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#include "bsp.h"
#include "adc_config.h"
#include "stm32f4xx_hal.h"


// 4路ADC均接在GPIOA上，接在ADC1上，只需要使能GPIOA和ADC1时钟即可
 #define ADC_PR_Pin          GPIO_PIN_0
 #define ADC_PR_GPIO_Port    GPIOA

 #define ADC_24V_Pin         GPIO_PIN_1
 #define ADC_24V_GPIO_Port   GPIOA

 #define ADC_36V_Pin         GPIO_PIN_2
 #define ADC_36V_GPIO_Port   GPIOA

 #define ADC_5V_Pin          GPIO_PIN_3
 #define ADC_5V_GPIO_Port    GPIOA

// ADC序号
#define VOLTAGE_ADC          ADC1

#define CHANNEL_PR      ADC_CHANNEL_0
#define CHANNEL_24V     ADC_CHANNEL_1
#define CHANNEL_36V     ADC_CHANNEL_2
#define CHANNEL_5V      ADC_CHANNEL_3

// TODO: 暂时使用cubemx生成的驱动，基本未做通用性适配

ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
volatile uint16_t ADC_convert_value[ADC_GROUP1_TOTAL_CHANNEL];

void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle);

/* ADC1 init function */
void MX_ADC1_Init(void)
{
    ADC_ChannelConfTypeDef sConfig = {0};

    /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  */
    hadc1.Instance = VOLTAGE_ADC;
    hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
    hadc1.Init.Resolution = ADC_RESOLUTION_12B;
    hadc1.Init.ScanConvMode = ENABLE;
    hadc1.Init.ContinuousConvMode = ENABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = ADC_GROUP1_TOTAL_CHANNEL;
    hadc1.Init.DMAContinuousRequests = ENABLE;
    hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
    if (HAL_ADC_Init(&hadc1) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
    sConfig.Channel = CHANNEL_PR;
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
    sConfig.Channel = CHANNEL_24V;
    sConfig.Rank = 2;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
    sConfig.Channel = CHANNEL_36V;
    sConfig.Rank = 3;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
    /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  */
    sConfig.Channel = CHANNEL_5V;
    sConfig.Rank = 4;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
	
    //开启adc dma
    if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_convert_value, ADC_GROUP1_TOTAL_CHANNEL) != HAL_OK)
    {
        Error_Handler(__FILE__, __LINE__);
    }
}

void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle)
{

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    if (adcHandle->Instance == VOLTAGE_ADC)
    {
        /* ADC1 clock enable */
        __HAL_RCC_ADC1_CLK_ENABLE();

        __HAL_RCC_GPIOA_CLK_ENABLE();
        /**ADC1 GPIO Configuration
        PA0-WKUP     ------> ADC1_IN0
        PA1     ------> ADC1_IN1
        PA2     ------> ADC1_IN2
        PA3     ------> ADC1_IN3
        */
        GPIO_InitStruct.Pin = ADC_PR_Pin | ADC_24V_Pin | ADC_36V_Pin | ADC_5V_Pin;
        GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        // HAL_DMA_DeInit(&hdma_adc1); 加上这一句会有问题
        /* ADC1 DMA Init */
        /* ADC1 Init */
        hdma_adc1.Instance = DMA2_Stream0;
        hdma_adc1.Init.Channel = DMA_CHANNEL_0;
        hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
        hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
        hdma_adc1.Init.Mode = DMA_CIRCULAR;
        hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
        hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
        {
            Error_Handler(__FILE__, __LINE__);
        }

        __HAL_LINKDMA(adcHandle, DMA_Handle, hdma_adc1);
    }
}

void HAL_ADC_MspDeInit(ADC_HandleTypeDef *adcHandle)
{
    if (adcHandle->Instance == VOLTAGE_ADC)
    {
        /* Peripheral clock disable */
        __HAL_RCC_ADC1_CLK_DISABLE();

        /**ADC1 GPIO Configuration
        PA0-WKUP     ------> ADC1_IN0
        PA1     ------> ADC1_IN1
        PA2     ------> ADC1_IN2
        PA3     ------> ADC1_IN3
        */
        HAL_GPIO_DeInit(GPIOA, ADC_PR_Pin | ADC_24V_Pin | ADC_36V_Pin | ADC_5V_Pin);

        /* ADC1 DMA DeInit */
        HAL_DMA_DeInit(adcHandle->DMA_Handle);
    }
}

